﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Minestorm.Retro.GameLogic;

namespace Minestorm.Retro.Entities
{
    public class Bullet : Entity
    {
        private GraphicsDevice _graphics;
        private float _angle;
        private TimeSpan _born;
        private Vector2 _lastPosition;
        private Texture2D _texture;
        private SpriteBatch _spriteBatch;

        public TimeSpan Born { get { return _born; } }
        public Vector2 LastPosition { get { return _lastPosition; } }

        public Bullet(GraphicsDevice graphics, Vector2 position, float angle, TimeSpan gameTime, TextureManager textureManager)
            : base(graphics, position)
        {
            _graphics = graphics;
            _angle = angle;
            _born = gameTime;
            ShouldDraw = true;
            _texture = textureManager.GetTexture("bullet");
        }

        public override bool DrawVertices
        {
            get
            {
                return false;
            }
        }

        public override void Initialize()
        {
            var verticies = new VertexPositionTexture[4];
            verticies[0] = new VertexPositionTexture(new Vector3(-.15f, -.15f, 0), Vector2.Zero);
            verticies[1] = new VertexPositionTexture(new Vector3(.15f, -.15f, 0), new Vector2(_texture.Width, 0));
            verticies[2] = new VertexPositionTexture(new Vector3(.15f, .15f, 0), new Vector2(_texture.Width, _texture.Height));
            verticies[3] = new VertexPositionTexture(new Vector3(-.15f, .15f, 0), new Vector2(0, _texture.Height));

            SetVerticies(verticies);

            SetIndicies("bullet", false,
                0, 1, 2, 2, 3, 0
            );
            _spriteBatch = new SpriteBatch(_graphics);
        }

        public override void Draw(GameTime gameTime)
        {
            //DrawInternal(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
            var vec = _graphics.Viewport.Project(Vector3.Zero, Level.Projection, Level.View, GetWorld("star"));

            _spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
            _spriteBatch.Draw(_texture, new Rectangle((int)vec.X - (_texture.Width / 2), (int)vec.Y - (_texture.Height / 2), _texture.Width, _texture.Height), Constants.VectorColor);
            _spriteBatch.End();
        }

        public override Matrix GetWorld(string indexBufferName)
        {
            return Matrix.Identity * Matrix.CreateTranslation(new Vector3(Position, 0f));
        }

        public void Move()
        {
            _lastPosition = Position;

            Position = new Vector2(Position.X + (float)Math.Sin(-_angle) * Constants.Ship.BulletSpeed, Position.Y + (float)Math.Cos(-_angle) * Constants.Ship.BulletSpeed);

            Position = WrapBounds(Position);
        }
    }
}
